package com.cloud.zuul.utils;

import com.lwj.framework.constant.Constant;
import com.lwj.framework.entity.AuthEntity;
import org.springframework.util.Base64Utils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

public class OauthUtils {
    private static MultiValueMap<String, String> authorizHttpHeaders(String clientinfo) {
        MultiValueMap<String, String> head = new LinkedMultiValueMap<String, String>();
        StringBuilder headBuilder = new StringBuilder();
        headBuilder.append("Basic").append(Constant.BLANK_STRING).append(clientinfo);
        head.add("Authorization", headBuilder.toString());
        return head;
    }

    /**
     * 设置认证头部
     *
     * @param authEntity
     * @return
     */
    public static MultiValueMap<String, String> getHttpHeaders(AuthEntity authEntity) {
        StringBuilder clientinfo = new StringBuilder();
        clientinfo.append(authEntity.getClientId()).append(Constant.COLON_STRING).append(authEntity.getSecret());
        String base64Clientinfo = Base64Utils.encodeToString(clientinfo.toString().getBytes());
        return authorizHttpHeaders(base64Clientinfo);
    }
}
